
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>总仓盘点</title>
	<link href="../../rs/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
	<link href="../../rs/css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
	<link href="../../rs/plugins/bootstrap_tab/css/bootstrap-table.css" rel="stylesheet">
	<link href="../../rs/css/animate.min.css" rel="stylesheet">
	<link href="../../rs/css/style.min862f.css?v=4.1.0" rel="stylesheet">
	<link href="../../rs/plugins/datapicker/datepicker3.css" rel="stylesheet">
	<link href="../../rs/plugins/bootstrap-editable/css/bootstrap-editable.css" rel="stylesheet">
  <?php
    require_once('login.php');
   ?>
  <style>
         #toolbar1 {
             padding: 0px 15px;
             float: right;
         }
         .btn-group-sm > .btn, .btn-sm {
             padding: 5px 12px;
         }

         .editable-click, a.editable-click, a.editable-click:hover {
             border-bottom: 0px;
         }
         .fixed-table-pagination .page-list{
             display: none;
         }
     </style>
</head>

<body class="gray-bg">
  <div class="wrapper wrapper-content animated fadeInRight">
    <div class="ibox float-e-margins" style="    background: #fff;padding: 20px;">
      <div class="row">

        <div class="col-sm-4 m-b-xs"></div>
        <div class="col-sm-4 m-b-xs"></div>
        <div class="col-sm-4 m-b-xs">
          <div id="toolbar1">
              <button  onclick="cancelall()" style="float:left;margin-left:5px" class="btn" id="del">
                <i class="glyphicon glyphicon-remove"></i> 删除
              </button>
              <button onclick="save_do()" style="float:left;margin-left:5px" class="btn btn-success">
                  <i class="glyphicon glyphicon-ok"></i> 保存
              </button>
              <button onclick="save_sh()" style="float:left;margin-left:5px" class="btn btn-danger">
                  <i class="glyphicon glyphicon-zoom-in"></i> 审核
              </button>
          </div>
        </div>
      </div>

      <div class="row">
        <div class="col-sm-4 m-b-xs">
            <div class="form-group" id="data_1">
                <div class="input-group date">
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    <input type="text" class="form-control" placeholder="请选择单据日期" name="start" id="start" readonly="readonly">
                </div>
            </div>
        </div>
        <div class="col-sm-4 m-b-xs">
            <div class="form-group draggable">
                <div class="input-group">
                        <span class="input-group-btn">
                            <button style="height:34px;background-color: #FFF;border: 1px solid #e5e6e7;font-size: 14px;margin-left:-1px" type="button" class="btn btn-sm ">订单</button>
                        </span>
                    <select class="form-control" id="selectorder">
                        <option value="-1">新单</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-sm-4">
            <div class="input-group ">
                        <span class="input-group-btn">
                            <button style="height:34px;background-color: #FFF;border: 1px solid #e5e6e7;font-size: 14px;margin-left:-1px" type="button" class="btn btn-sm ">仓库</button>
                        </span>
                <select class="form-control" id="ckc">
                </select>
            </div>
        </div>


      </div>


      <div class="row">
          <div class="col-sm-12">
              <div class="input-group">
                  <div class="input-group-btn">
                      <button style="height:34px" type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown">
                          订单备注
                      </button>
                  </div>
                  <input type="text" class="form-control" id="ddbz">
              </div>
          </div>
      </div>
    </div>

      <!-- Panel Other -->

      <div class="ibox float-e-margins">
          <div class="ibox-content">
              <div class="row">
                  <div>
                      <div class="col-sm-4">
                        <button style="float:left;  margin-right:5px;margin-bottom: 20px;height:34px;" id="remove" class="btn btn-danger" disabled>
                            <i class="glyphicon glyphicon-trash" style="font-size:12px"></i>
                        </button>
                      </div>
                      <div class="col-sm-4 m-b-xs" style="float: right;">
                          <div class="input-group">
                            <div class="input-group-btn">
                                <button style="height:34px;margin-left:-1px;" type="button" class="btn btn-white dropdown-toggle" onclick="open_modal()">
                                    产品资料
                                </button>
                                <ul class="dropdown-menu dropdown-menu-right" role="menu"></ul>
                            </div>
                              <input type="text" class="form-control" id="searchtxt" onkeydown="_key()">
                              <div class="input-group-btn">
                                  <button style="height:34px" type="button" class="btn btn-white dropdown-toggle" onclick="open_modal()">
                                      搜索
                                  </button>
                                  <ul class="dropdown-menu dropdown-menu-right" role="menu"></ul>
                              </div>
                          </div>
                      </div>
                  </div>
              </div>
              <table id="table"></table>
              <!-- End Example Card View -->
          </div>
      </div>
  </div>


<script src="../../rs/js/jquery.min.js"></script>
<script src="../../rs/js/bootstrap.js"></script>
<script src="../../rs/plugins/bootstrap_tab/js/bootstrap-table.min.js"></script>
<script src="../../rs/plugins/bootstrap_tab/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="../../rs/plugins/datapicker/bootstrap-datepicker.js"></script>
<script src="../../rs/js/jquery.cookie.js"></script>
<script src="../../rs/js/common_qj.js"></script>
<script src="../../rs/plugins/layer/layer.min.js"></script>
<script src="../../rs/plugins/bootstrap-editable/js/bootstrap-editable.js"></script>
<script src="../../rs/plugins/bootstrap-editable/js/bootstrap-table-editable.min.js"></script>


 <script type="text/javascript">
     var $table = $('#table');
     var $remove = $('#remove');
     var sign = URLencode($.cookie('sign'));

     $(function () {
         //日期选择插件
         $("#data_1 .input-group.date").datepicker({
             todayBtn: "linked",
             keyboardNavigation: !1,
             forceParse: !1,
             calendarWeeks: !0,
             autoclose: !0
         });

         var start = (new Date()).format('yyyy-MM-dd');
         $('#start').val(start);
				 $('#table').bootstrapTable({
						 columns: [
								 {
										 field: 'state',
										 checkbox: true
								 },
								 {
										 field: 'prid',
										 title: '商品编号',
										 align: 'left',

								 },
								 {
										 field: 'prmc',
										 align: 'left',
										 title: '商品名称'
								 },
								//  {
								// 		 field: 'dj',
								// 		 align: 'right',
								// 		 title: '单价',
								//  },
								 {
										 field: 'pds',
										 align: 'right',
										 title: '盘点数',
										 editable: true
								 },
								{
										field: 'kcs',
										align: 'right',
										title: '库存数',
								},
								 {
										 field: 'cys',
										 align: 'right',
										 title: '差异数',
										 formatter: fth

								 }
						 ],
						 advancedSearch: true,
						 idTable: "advancedTable",
						 shadeClose: true,
						 toggle: "table",
						 height: "299",

						 //当修改数值后执行的保存操作
						 onEditableSave: function (field, row, oldValue, $el) {


								 if (field == 'pds') {
										 if (isNaN(row.pds)) {
												 row.pds = oldValue;
										 }else if(row.pds==""){
											row.pds = oldValue;
										}else if(parseInt(row.pds)>parseInt(row.kc)){
											layer.msg("库存数小于当前输入！");
											row.pds = oldValue;
										}else {
												 row.pds=parseFloat(row.pds).toFixed(2);
										 }
								 }

								 $('#table').bootstrapTable('load', dtn);
								 return true;
						 }
				 });
        f_sheard();

				<!-- 删除操作 -->
        $table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function () {
            $remove.prop('disabled', !$table.bootstrapTable('getSelections').length);
        });
				$remove.click(function () {
						layer.confirm('确定要删除这个商品吗？', {
								title: [''],
								btn: ['确定', '取消'] //按钮
						}, function (index) {
							var prids = $.map($table.bootstrapTable('getSelections'), function (row) {
									return row.prid;
							});
							layer.close(index);
							$table.bootstrapTable('remove', {
									field: 'prid',
									values: prids
							 });
							 var data_sy=$table.bootstrapTable('getData');

							 if(data_sy.length=='0'){
								 $("#ckc").attr('disabled',false);
							 };

						}, function (index) {
								layer.close(index);
						});
				});
				<!-- 删除操作 -->

         //隐藏按钮
         var cz1 = $("#selectorder  option:selected").text();
         if(cz1=='新单'){

						 $('#del').attr("disabled",true);

         }
     });

      var slpr = {};
     function f_sheard() {
			 $('#ckc').attr('disabled',false);
         layer.load(2);
				 //给仓库做循环
				 $.ajax({
						 type: "post",
						 url:G_url+'sysg/see',
						 data: 'sign=' + sign,
						 dataType: "json",
						 success: function (result) {
								 layer.closeAll('loading');
								 var ff = result.data;
								 var html1 = '';
								 $.each(ff, function (a, b) {//直接取出json的prolist做each循环.
										 html1 += "<option value='" + b.ShopGroupID + "'>" + b.ShopGroupName + "</option>";//拼接字符串.
								 });
								 $("#ckc").append(html1);
								  $("#rkc").append(html1);
								 //$("#mdbh1").append(html1);
						 },
						 error: function (XMLHttpRequest, textStatus, errorThrown) {
						 }
				 });



				 //订单order
				 $.ajax({
						 type: "post",
						 url:G_url+'stcho/news'+'?sign='+sign,
						 data: 'sign=' + sign,
						 dataType: "json",
						 success: function (result) {
							 layer.closeAll('loading');
							 var ff = result.data.dt.dh;
							 var html1 = '';
							 $.each(ff, function (a, b) {//直接取出json的prolist做each循环.
									 html1 += "<option value='" + b.djbh + "'>" + b.djbh + "</option>";//拼接字符串.
							 });

							 $("#selectorder").append(html1);
						 },
						 error: function (XMLHttpRequest, textStatus, errorThrown) {
						 }
				 });

     }


     //给button添加回车事件
     function _key() {
         if(event.keyCode ==13){
             open_modal();
             $("#searchtxt").blur();
         }
     }

		 function open_modal() {
         var s = $('#searchtxt').val();
				 //产品资料
				 var ckc_name = $("#ckc  option:selected").val();
				 $.ajax({
						 type: "post",
						 url:G_url+'stino/seeslpr/'+ckc_name,
						 data: 'sign=' + sign,
						 dataType: "json",
						 success: function (result) {
								 layer.closeAll('loading');
								 if (result.ok) {
										slpr =result.data;
							$('#table1').bootstrapTable('destroy');//删除table表格
							$('#table1').bootstrapTable({//重新建立表格
									columns: [
											{
													field: 'prid',
													title: '商品编号'
											},
											{
													field: 'prname1',
													title: '商品名称'
											},
											{
													field: 'prpricein',
													title: '单价'
											},
											{
													field: 'kc',
													title: '库存'
											},
											{
													field: 'insert',
													title: '添加',
													align: 'center',
													formatter: insertFormatter
											}
									],
									shadeClose: true,
									searchText: s,
									data: slpr,
									toggle: "table",
									
									search: true
							});

								} else {
									layer.msg(result.msg);
								}
						 },
						 error: function (XMLHttpRequest, textStatus, errorThrown) {
						 }
				 });

         //页面层
         layer.open({
             title: ['添加商品'],
             type: 1,
             offset: 'rb',//top顶部偏移

             area: ['60%', '100%'], //宽高
             shadeClose: true,
             content: '<div style="padding: 20px 30px 0 30px "><table id="table1"></table></div>'
         });



     }

		 function insert_do(index) {
			 var r=slpr[index];
 			 $('#ckc').attr('disabled',true);

 				var lno = 0;
 				var sfcz = false;
 				for (var i = 0, l = dtn.length; i < l; i++) {
 						if (dtn[i].prid == r.prid) {
 								lno = i;
 								sfcz = true;
 								break
 						}
 				}

 				if (sfcz) {
 						layer.open({
 								content: '数据存已在是否累加?',
 								title: [''],
 								btn: ['累加', '新增'], //按钮
 								yes: function (index) {
 										//累加
 										dtn[lno].pds++;
 										$table.bootstrapTable('load', dtn);
 										layer.close(index);
 								},
 								btn2: function (index) {
 										//新增
 										var n = {};
 										n.prid = r.prid;
 										n.prmc = r.prname1;
 										n.dj = r.prpricein;
 										n.kcs = r.kc;

 										n.pds = 1;
 										n.rid= 0;
										n.bz="";
 										dtn.push(n);
 										$table.bootstrapTable('load', dtn);
 										layer.close(index);
 								},
 								cancle: function (index) {
 										layer.close(index);
 								}
 						});
 				} else {
 						//新增
 						var n = {};
 						n.prid = r.prid;
 						n.prmc = r.prname1;
 						n.dj = r.prpricein;
 						n.kcs = r.kc;
 						n.pds = 1;
 						n.rid= 0;
						n.bz="";
 						dtn.push(n);
 						$table.bootstrapTable('load', dtn);
 				}

 		}

     //使用函数将调用的那一行的样式写出
     function insertFormatter(value, row,index) { 
         return '<button type="button" class="btn btn-info" onclick="insert_do('+ index +')">添加</button> ';  
     }


     var kk=-1;
    


		$("#selectorder").change(function () {
			var cz = $("#selectorder  option:selected").text();
			var cv = $("#selectorder  option:selected").val();
			kk=cv;
			if(cz=='新单'){
			 $('#del').attr("disabled",true);
			location.reload();
			}else {
				$('#del').attr("disabled",false);
			}
			f_detail(cz);

		});



     var dtn = [];
     var sign1=$.cookie('sign');
     function f_detail(djbh) {
         layer.msg('数据加载中...', {icon: 16, time: 200000, shade: [0.1, 'black']});
         $.ajax({
             type: "post",
             url: G_url+'stcho/load/'+djbh,
             data:  'sign=' + sign,
             dataType: "json",
             success: function (result) {
                 layer.closeAll();
                 if (!result.ok) {
                     layer.msg(result.msg);

                 }
                 dtn = result.detail;


								 $("#start").val(result.order.djrq);
								//仓库
								var ck_val=result.order.ckbh;
								 $("#m_ck").find("option[value="+ck_val+"]").attr("selected",true);
								 //客户资料
 								var kh_val=result.order.khbh;
 								 $("#m_kh").find("option[value="+kh_val+"]").attr("selected",true);

								 //订单备注赋值
								  $("#ddbz").val(result.order.bz);
                 $('#table').bootstrapTable('load', dtn);
                //  $("#mdbh").val(result.order.shop);
                //  $("#mdbh1").val(result.order.shop2);
                 banhao();
             },
             error: function (XMLHttpRequest, textStatus, errorThrown) {
             }
         });
     }

     function cancelall(){
         layer.confirm('确定删除这个订单吗？', {
             title: [''],
             btn: ['确定', '取消'] //按钮
         }, function (index) {
             layer.msg('数据删除中...', {icon: 16, time: 200000, shade: [0.1, 'black']});
             var djbh_del = $("#selectorder  option:selected").text();
             $.ajax({
                 type: "post",
                 url: G_url+'stcho/del/'+djbh_del,
                 data:'sign=' + sign,
                 dataType: "json",
                 success: function (result) {
                     layer.closeAll();
										location.reload();

                 },
                 error: function (XMLHttpRequest, textStatus, errorThrown) {
                     layer.msg('数据删除失败', {time: 1000});
                 }
             });
             layer.close(index);
         }, function (index) {
             layer.close(index);
         });
     }

     var f_data={};
		 function save_do(){
 			 var ckc=$("#ckc option:selected").val();
 			 var rkc=$("#rkc option:selected").val();
			 var djbh=$("#selectorder  option:selected").text();
			 var bz=$('#ddbz').val();

 			 if(ckc==rkc){
 				 layer.msg("出库仓和入库仓不能相同！");
 				 return;
 			 }
 					 layer.confirm('确定要保存吗？', {
 							 title: [''],
 							 btn: ['确定', '取消'] //按钮
 					 }, function (index) {

 							 var djrq=$('#start').val();


 							 var data=[{
 									 "djrq":djrq,  //日期
 									 "ckbh":ckc,  //入库仓
 									 "ckbh2":rkc,  //出库仓
									 "djbh":djbh,//单据编号
 									 "bz":bz,    //备注
 									 djje:'0'
 							 }];
 							 var data1=$('#table').bootstrapTable('getData');
 							 var save={};
 							 save.order=data;
 							 save.detail=data1;
 							//  save.sign=sign1;
 							 $.ajax({
 									 type: "post",
 									 url: G_url+'stcho/save'+"?sign="+sign,
 									 data:JSON.stringify(save),
 									 dataType: "json",
 									 success: function(res) {
										 if (res.ok){
												 layer.msg(res.msg);
												 f_data=res.detail;

												 //var ss = dtnheard.order;
												 if(djbh=="新单"){
														 $("#selectorder").append( "<option value='"+ res.djbh+"' selected>" + res.djbh + "</option>");
												 }

												 //$("#del").show();
												 $('#del').attr("disabled",false);

												 $('#table').bootstrapTable('load', f_data);
										 } else {
												 layer.msg(res.msg);

										 }
 									 },
 									 error: function (XMLHttpRequest, textStatus, errorThrown) {
 											 layer.msg('数据保存失败', {time: 1000});
 									 }
 							 });
 							 layer.close(index);
 					 }, function (index) {
 							 layer.close(index);
 					 });

 	 }

   

     function save_sh(){
			 var djbhsh=$("#selectorder  option:selected").text();
			 if(djbhsh=="新单"){
					layer.msg("请选择要审核的订单！");
					return;
			 }
         layer.confirm('确定提交审核吗？', {
             title: [''],
             btn: ['确定', '取消'] //按钮
         }, function (index) {
             //sh();
             layer.msg('数据提交中...', {icon: 16, time: 200000, shade: [0.1, 'black']});

             $.ajax({
                 async: false,
                 type: "post",
                 url: G_url+'stcho/sh/'+djbhsh,
                 data:'sign=' + sign,
                 dataType: "json",
                 success: function(res) {
                     if (res.ok){
                         layer.msg(res.msg);
                         location.reload();
                     } else {
                         layer.msg(res.msg);

                     }
                 },
                 error: function (XMLHttpRequest, textStatus, errorThrown) {
                     layer.msg('数据提交失败', {time: 1000});
                 }
             });
             layer.close(index);
         }, function (index) {
             layer.close(index);
         });
     }


     //排序功能
function banhao(){
	var len = $('#table tbody tr').length;
	for(var i = 0;i<len;i++){
		$('table tbody tr:eq('+i+') td:nth-child(2)').text(i+1);
	}
}

//小计计算
function fth(value, row, index) {
		var sum =parseInt(row.pds) - parseInt(row.kcs);
		return sum.toFixed(2);
}


 </script>
</body>
</html>
